package com.suneast.suneaststore.modules.goodsdetail.adapter;

import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.suneast.suneaststore.R;
import com.suneast.suneaststore.bean.homepage.RecommendProductBean;

/**
 * Created by zestz on 2018/3/7.
 */

public class ProductAdapter extends BaseQuickAdapter<RecommendProductBean, BaseViewHolder> {

    public ProductAdapter(int layoutResId) {
        super(layoutResId);
    }

    @Override
    protected void convert(BaseViewHolder helper, RecommendProductBean item) {
        Glide.with(helper.itemView.getContext()).load(item.getThumb())
                .into((ImageView) helper.getView(R.id.img_goods));
        helper.setText(R.id.product_intro_tv, item.getTitle());
        if (item.getDiscount() > 0) {
            int realPriceLength = item.getPrice() == null ? 0 : item.getPrice().length();
            String discountStr = String.valueOf(item.getDiscount());
            int disCountPriceLength = discountStr.length();
            Spannable multiplePriceSpan = new SpannableString(String.format(
                    helper.itemView.getResources().getString(R.string.search_multiple_price),
                    item.getPrice(), item.getUnit(), discountStr, item.getUnit()));
            multiplePriceSpan.setSpan(new AbsoluteSizeSpan(40), 0, realPriceLength,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            multiplePriceSpan.setSpan(new ForegroundColorSpan(Color.RED), 0,
                    realPriceLength, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            multiplePriceSpan.setSpan(new AbsoluteSizeSpan(40), realPriceLength + 5,
                    realPriceLength + 7 + disCountPriceLength,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            multiplePriceSpan.setSpan(new ForegroundColorSpan(Color.RED), realPriceLength + 5,
                    realPriceLength + 7 + disCountPriceLength,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            helper.setText(R.id.product_intro_tv, item.getTitle())
                    .setText(R.id.price_value_tv, multiplePriceSpan);
        } else {
            int memberPriceLength = item.getPrice() == null ? 0 : item.getPrice().length();
            Spannable onePriceSpan = new SpannableString(String.format(
                    helper.itemView.getResources().getString(R.string.search_total_price),
                    item.getPrice(), item.getUnit()));
            onePriceSpan.setSpan(new AbsoluteSizeSpan(40), 0,
                    memberPriceLength + 1,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            onePriceSpan.setSpan(new ForegroundColorSpan(Color.RED), 0,
                    memberPriceLength, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            helper.setText(R.id.product_intro_tv, item.getTitle())
                    .setText(R.id.price_value_tv, onePriceSpan);
        }
    }
}
